//
//  LLNWHTTPFileHandler.m
//  LLFoundation
//
//  Created by my_liu on 2016/11/14.
//  Copyright © 2016年 . All rights reserved.
//

#import "LLNWHTTPFileHandler.h"
#import <CommonCrypto/CommonDigest.h>
#import "UIButton+AFNetworking.h"
#import "UIImageView+AFNetworking.h"

#define kLLNWPhotoFolderCache   @"photoCaches"

@interface LLNWHTTPFileHandler ()

+ (NSString *)MD5:(NSString *)sender;

//+ (NSString *)getFilePathOfCache:(NSString *)strName;

@end

@implementation LLNWHTTPFileHandler

+(instancetype)shareInstance
{
    static LLNWHTTPFileHandler * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        manager = [[self alloc]init];
    });
    return manager;
}

+ (NSString *)MD5:(NSString *)sender {
    
    if (sender && [sender isKindOfClass:[NSString class]])
    {
        if (0 < sender.length)
        {
            const char *cStr = [sender UTF8String];
            unsigned char result[CC_MD5_DIGEST_LENGTH];
            CC_MD5(cStr,(CC_LONG)strlen(cStr),result);
            
            return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",result[0],result[1],result[2],result[3],result[4],result[5],result[6],result[7],
                    result[8],result[9],result[10],result[11],result[12],result[13],result[14],result[15]];
        }
    }
    
    return @"";
}

//生成图片缓存路径
- (NSString *)makePhotoCachePathWithUrl:(NSString *)url {
    
    NSString *strMD5 = [LLNWHTTPFileHandler MD5:url];
    
    if (strMD5 && 0 < strMD5.length)
    {
        NSString *folderPath = [self getFilePathOfCache:kLLNWPhotoFolderCache];
        
        return [folderPath stringByAppendingPathComponent:strMD5];
    }
    
    return nil;
}

//获取移动设备的Cache路径
-(NSString *)getFilePathOfCache:(NSString *)cacheKey {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:cacheKey];
}

-(void)createLastDirectory:(NSString *)path
{
    
    NSString *lastDir = [path stringByDeletingLastPathComponent];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:lastDir isDirectory:nil]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:lastDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
}

- (BOOL)saveImage:(UIImage *)image withUrl:(NSURL *)url
{
    if(!image || !url){
        
        return NO;
    }
    
    NSString * photoCachePath = [self makePhotoCachePathWithUrl:[url absoluteString]];
    
    [self createLastDirectory:photoCachePath];
    
    return [NSKeyedArchiver archiveRootObject:image toFile:photoCachePath];
    
}

- (UIImage *)getImageWithUrl:(NSURL *)url
{
    NSString * photoCachePath = [self makePhotoCachePathWithUrl:[url absoluteString]];
    
    id imgObj = [NSKeyedUnarchiver unarchiveObjectWithFile:photoCachePath];
    if (imgObj && [[imgObj class] isSubclassOfClass:[UIImage class]]) {
        return  (UIImage *)imgObj;
    }
    return nil;
    
}

- (BOOL)clearImageCacheWithUrl:(NSURL *)url
{
    NSString * photoCachePath = [self makePhotoCachePathWithUrl:[url absoluteString]];
    
    
    NSError *error = nil;
    BOOL isSuccess = [[NSFileManager defaultManager] removeItemAtPath:photoCachePath error:&error];
    if (!isSuccess) {
        return NO;
    }
    return YES;
    
}

- (BOOL)clearAllImageCache
{
    NSString *folderPath = [self getFilePathOfCache:kLLNWPhotoFolderCache];
    
    NSError *error = nil;
    BOOL isSuccess = [[NSFileManager defaultManager] removeItemAtPath:folderPath error:&error];
    if (!isSuccess) {
        return NO;
    }
    return YES;
    
}

@end
